package cn.ibizlab.odoo.core.odoo_im_livechat.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule;
import cn.ibizlab.odoo.core.odoo_im_livechat.domain.Im_livechat_channel_rule;
import cn.ibizlab.odoo.core.odoo_im_livechat.filter.Im_livechat_channel_ruleSearchContext;
import cn.ibizlab.odoo.core.odoo_im_livechat.service.IIm_livechat_channel_ruleService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[实时聊天频道规则] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Im_livechat_channel_ruleServiceImpl implements IIm_livechat_channel_ruleService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iim_livechat_channel_ruleClientService im_livechat_channel_ruleClientService;

    @Override
    public void updateBatch(List<Im_livechat_channel_rule> im_livechat_channel_rules, int batchSize) {

        //im_livechat_channel_ruleClientService.updateBatch(im_livechat_channel_rules,batchSize) ;

    }

    @Override
    public Im_livechat_channel_rule get(Integer id) {
        Iim_livechat_channel_rule clientModel = im_livechat_channel_ruleClientService.createModel();
        clientModel.setId(id);
        im_livechat_channel_ruleClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Im_livechat_channel_rule get(Im_livechat_channel_rule im_livechat_channel_rule) {
        Im_livechat_channel_rule retim_livechat_channel_rule = this.get(im_livechat_channel_rule.getId()) ;
    	BeanCopier copier=BeanCopier.create(retim_livechat_channel_rule.getClass(), im_livechat_channel_rule.getClass(), false);
        copier.copy(retim_livechat_channel_rule, im_livechat_channel_rule, null);
        return im_livechat_channel_rule ;
    }

    @Override
    public void createBatch(List<Im_livechat_channel_rule> im_livechat_channel_rules, int batchSize) {

                        //im_livechat_channel_ruleClientService.createBatch(im_livechat_channel_rules,batchSize) ;

    }

    @Override
    public boolean update(Im_livechat_channel_rule im_livechat_channel_rule) {
        boolean bOk = false ;
        Iim_livechat_channel_rule clientModel = this.convert2Model(im_livechat_channel_rule,null);
        im_livechat_channel_ruleClientService.update(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_channel_rule) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Im_livechat_channel_rule im_livechat_channel_rule) {
        boolean bOk = false ;
        Iim_livechat_channel_rule clientModel = this.convert2Model(im_livechat_channel_rule,null);
        im_livechat_channel_ruleClientService.create(clientModel) ;
        this.convert2Domain(clientModel,im_livechat_channel_rule);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iim_livechat_channel_rule clientModel = im_livechat_channel_ruleClientService.createModel();
        clientModel.setId(id);
        im_livechat_channel_ruleClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //im_livechat_channel_ruleClientService.removeBatch(im_livechat_channel_rules) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Im_livechat_channel_rule> searchDefault(Im_livechat_channel_ruleSearchContext context) {
        List<Im_livechat_channel_rule> list = new ArrayList<Im_livechat_channel_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule> clientModelList = im_livechat_channel_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel_rule>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据即时聊天查询
     */
    @Override
    public Page<Im_livechat_channel_rule> selectByOdoo_channel(Integer id){
        Im_livechat_channel_ruleSearchContext context = new Im_livechat_channel_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_channel_rule> list = new ArrayList<Im_livechat_channel_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule> clientModelList = im_livechat_channel_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Im_livechat_channel_rule> selectByOdoo_create(Integer id){
        Im_livechat_channel_ruleSearchContext context = new Im_livechat_channel_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_channel_rule> list = new ArrayList<Im_livechat_channel_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule> clientModelList = im_livechat_channel_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Im_livechat_channel_rule> selectByOdoo_write(Integer id){
        Im_livechat_channel_ruleSearchContext context = new Im_livechat_channel_ruleSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Im_livechat_channel_rule> list = new ArrayList<Im_livechat_channel_rule>();
        Page<cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule> clientModelList = im_livechat_channel_ruleClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iim_livechat_channel_rule clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Im_livechat_channel_rule>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iim_livechat_channel_rule convert2Model(Im_livechat_channel_rule domain , Iim_livechat_channel_rule model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = im_livechat_channel_ruleClientService.createModel() ;
        }

         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getAuto_popup_timerDirtyFlag())
            model.setAuto_popup_timer(domain.getAuto_popup_timer());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getRegex_urlDirtyFlag())
            model.setRegex_url(domain.getRegex_url());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getActionDirtyFlag())
            model.setAction(domain.getAction());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCountry_idsDirtyFlag())
            model.setCountry_ids(domain.getCountry_ids());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getChannel_id_textDirtyFlag())
            model.setChannel_id_text(domain.getChannel_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getChannel_idDirtyFlag())
            model.setChannel_id(domain.getChannel_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Im_livechat_channel_rule convert2Domain( Iim_livechat_channel_rule model ,Im_livechat_channel_rule domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Im_livechat_channel_rule();
        }

        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getAuto_popup_timerDirtyFlag())
            domain.setAuto_popup_timer(model.getAuto_popup_timer());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getRegex_urlDirtyFlag())
            domain.setRegex_url(model.getRegex_url());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getActionDirtyFlag())
            domain.setAction(model.getAction());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCountry_idsDirtyFlag())
            domain.setCountry_ids(model.getCountry_ids());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getChannel_id_textDirtyFlag())
            domain.setChannel_id_text(model.getChannel_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getChannel_idDirtyFlag())
            domain.setChannel_id(model.getChannel_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
